package cn.sunlight.mm.wx.service.impl;

import cn.sunlight.framework.annotation.HmComponent;
import cn.sunlight.mm.database.MmDaoException;
import cn.sunlight.mm.database.SqlSessionUtils;
import cn.sunlight.mm.pojo.WxMember;
import cn.sunlight.mm.wx.dao.WxMemberDao;
import cn.sunlight.mm.wx.service.WxMemberService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

import java.util.Map;

/**
 * @author sunlight
 * @description:
 * @date 2021/6/1
 * @Version v1.0
 */
@HmComponent("memberService")
@Slf4j
public class WxMemberServiceImpl implements WxMemberService {

    @Override
    public WxMember findWxMemberByOpenId(String openId) {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        WxMemberDao wxMemberDao = sqlSession.getMapper(WxMemberDao.class);
        WxMember wxMember=wxMemberDao.selectByOpenId(openId);
        sqlSession.close();
        return wxMember;
    }

    @Override
    public void add(WxMember wxMember) {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        try {
            WxMemberDao wxMemberDao = sqlSession.getMapper(WxMemberDao.class);
            Integer result=wxMemberDao.addWxMember(wxMember);
            sqlSession.commit();
            sqlSession.close();
        }catch (MmDaoException e){
            sqlSession.rollback();
            sqlSession.close();
            e.printStackTrace();
            log.error(e.getMessage());
            throw new MmDaoException(e.getMessage());
        }

    }

    @Override
    public void updateCityCourse(Map updateData) {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        try {
            WxMemberDao wxMemberDao = sqlSession.getMapper(WxMemberDao.class);
            wxMemberDao.updateCityCourse(updateData);
            sqlSession.commit();
            sqlSession.close();
        }catch (MmDaoException e){
            sqlSession.rollback();
            sqlSession.close();
            e.printStackTrace();
            log.error(e.getMessage());
            throw new MmDaoException(e.getMessage());
        }

    }

}
